package com.mobclick.android;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.Handler;
import android.os.StatFs;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.lang.reflect.Field;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;

public class f
{
  public static final int a = 30000;
  private static Notification h = null;
  private static final int o = 8192;
  private Context b;
  private int c = 50;
  private String d;
  private String e;
  private String f;
  private String g;
  private NotificationManager i;
  private int j;
  private boolean k = true;
  private boolean l = true;
  private int m = j.a;
  private Handler n;
  private long p;
  private long q;
  private long r;
  private boolean s;
  private long t;

  public f(Context paramContext, String paramString1, String paramString2, String paramString3, String paramString4, String paramString5)
  {
    this.n = new g(this);
    this.r = 0L;
    this.s = false;
    try
    {
      a(paramContext, paramString1, paramString2, paramString3, paramString4, paramString5);
      return;
    }
    catch (Exception localException)
    {
      if (UmengConstants.testMode)
      {
        Log.e("MobclickAgent", "DownloadAgent init error");
        localException.printStackTrace();
      }
      this.k = false;
    }
  }

  private int a(String paramString1, String paramString2)
  {
    int i1;
    Field localField;
    try
    {
      localField = Class.forName(this.b.getPackageName() + ".R$" + paramString1).getField(paramString2);
      int i2 = Integer.parseInt(localField.get(localField.getName()).toString());
      i1 = i2;
      return i1;
    }
    catch (Exception localException)
    {
      if (UmengConstants.testMode)
        Log.e("MobclickAgent", "getIdByReflection error\n" + localException.getMessage());
      i1 = 0;
    }
  }

  public static String a(String paramString1, String paramString2, long paramLong)
  {
    return paramString1 + "_" + paramLong + "_" + paramString2 + ".apk";
  }

  private void a(Context paramContext, String paramString1, String paramString2, String paramString3, String paramString4, String paramString5)
  {
    this.b = paramContext;
    this.g = paramString5;
    e();
    if (this.m == j.b)
      this.k = false;
    while (true)
    {
      return;
      File localFile = Environment.getExternalStorageDirectory();
      this.d = localFile.getParent() + "/" + localFile.getName() + "/download";
      this.f = paramString1;
      this.j = 17301633;
      h = new Notification(this.j, paramString2, 1L);
      Notification localNotification = h;
      localNotification.flags = (0x2 | localNotification.flags);
      RemoteViews localRemoteViews = new RemoteViews(this.b.getPackageName(), a("layout", "umeng_analyse_download_notification"));
      localRemoteViews.setProgressBar(a("id", "umeng_analyse_progress_bar"), 100, 0, false);
      localRemoteViews.setTextViewText(a("id", "umeng_analyse_progress_text"), "0%");
      localRemoteViews.setTextViewText(a("id", "umeng_analyse_title"), paramString3);
      localRemoteViews.setTextViewText(a("id", "umeng_analyse_description"), paramString4);
      localRemoteViews.setImageViewResource(a("id", "umeng_analyse_appIcon"), this.j);
      h.contentView = localRemoteViews;
      Intent localIntent = new Intent();
      h.contentIntent = PendingIntent.getActivity(this.b, 0, localIntent, 134217728);
      this.i = ((NotificationManager)this.b.getSystemService("notification"));
    }
  }

  private void b(int paramInt)
  {
    long l1 = this.r;
    this.r = (1L + l1);
    if (l1 % this.c == 0L)
    {
      int i1 = (int)(100.0F * paramInt / (float)this.q);
      h.contentView.setProgressBar(a("id", "umeng_analyse_progress_bar"), 100, i1, false);
      h.contentView.setTextViewText(a("id", "umeng_analyse_progress_text"), String.valueOf(paramInt) + "%");
      this.i.notify(0, h);
    }
  }

  public static boolean b()
  {
    int i1;
    if (h == null)
      i1 = 0;
    while (true)
    {
      return i1;
      i1 = 1;
    }
  }

  private void c(boolean paramBoolean)
  {
    File localFile;
    i locali2;
    a locala = a.a("DownloadTask");
    HttpResponse localHttpResponse = locala.execute(new HttpGet(this.f));
    this.q = localHttpResponse.getEntity().getContentLength();
    this.e = a(this.b.getPackageName(), this.g, this.q);
    if (paramBoolean)
    {
      localFile = new File(this.d, this.e);
      if ((!(localFile.exists())) || (this.q != localFile.length()) || (!(UmengConstants.enableCacheInUpdate)))
        break label164;
      Log.i("MobclickAgent", "user exits apk!");
      this.k = true;
      this.m = j.a;
    }
    while (true)
    {
      while (true)
      {
        return;
        this.d = this.b.getFilesDir().getAbsolutePath();
        localFile = new File(this.d, this.e);
      }
      label164: long l1 = b(paramBoolean);
      if (this.q - localFile.length() <= l1)
        break;
      this.k = false;
      locala.a();
      this.m = j.e;
    }
    try
    {
      i locali1 = new i(this, localFile, "rw");
      locali2 = locali1;
    }
    catch (FileNotFoundException localFileNotFoundException)
    {
      try
      {
        InputStream localInputStream = localHttpResponse.getEntity().getContent();
        this.i.notify(0, h);
        int i1 = a(localInputStream, locali2);
        if ((this.p + i1 != this.q) && (this.q != -1L) && (this.k))
        {
          throw new IOException("Download incomplete: " + i1 + " != " + this.q);
          localFileNotFoundException = localFileNotFoundException;
          Log.v(null, "OutputStream Error");
          locali2 = null;
        }
      }
      catch (IOException localIOException)
      {
        while (true)
        {
          while (true)
          {
            this.k = false;
            locala.a();
            this.m = j.f;
          }
          locali2.close();
          locala.a();
          this.k = true;
          this.m = j.a;
        }
      }
    }
  }

  private void d()
  {
    this.i.cancel(0);
    h = null;
  }

  private void e()
  {
    if (!(f()))
    {
      Toast.makeText(this.b, g(), 3).show();
      this.m = j.b;
    }
    while (true)
    {
      while (true)
      {
        return;
        if (Environment.getExternalStorageState().equals("mounted"))
          break;
        this.m = j.c;
      }
      this.m = j.a;
    }
  }

  private boolean f()
  {
    boolean bool1;
    NetworkInfo localNetworkInfo;
    try
    {
      localNetworkInfo = ((ConnectivityManager)this.b.getSystemService("connectivity")).getActiveNetworkInfo();
      if (localNetworkInfo != null)
      {
        boolean bool2 = localNetworkInfo.isConnectedOrConnecting();
        bool1 = bool2;
        return bool1;
      }
      bool1 = false;
    }
    catch (Exception localException)
    {
      if (UmengConstants.testMode)
      {
        Log.e("MobclickAgent", "DownloadAgent inonline error");
        localException.printStackTrace();
      }
      bool1 = false;
    }
  }

  private String g()
  {
    return this.b.getString(a("string", "UMBreak_Network"));
  }

  public int a(InputStream paramInputStream, RandomAccessFile paramRandomAccessFile)
  {
    int i3;
    byte[] arrayOfByte = new byte[8192];
    BufferedInputStream localBufferedInputStream = new BufferedInputStream(paramInputStream, 8192);
    int i1 = 0;
    long l1 = -1L;
    try
    {
      boolean bool = this.s;
      label172: label184: i3 = i1;
    }
    finally
    {
      try
      {
        paramRandomAccessFile.close();
      }
      catch ( localObject)
      {
        try
        {
          while (true)
          {
            do
            {
              while (true)
              {
                int i2;
                while (true)
                {
                  localBufferedInputStream.close();
                  return i3;
                  i2 = localBufferedInputStream.read(arrayOfByte, 0, 8192);
                  if (i2 != -1)
                    break;
                  i3 = i1;
                }
                paramRandomAccessFile.write(arrayOfByte, 0, i2);
                i1 += i2;
                if (f())
                  break;
                this.s = true;
                i3 = i1;
              }
              if (this.t != 0L)
                break label184;
              if (l1 <= 0L)
                break label172;
            }
            while (System.currentTimeMillis() - l1 <= 30000L);
            this.m = j.g;
            this.s = true;
          }
          localObject = finally;
        }
        catch (IOException localIOException1)
        {
          try
          {
            paramRandomAccessFile.close();
          }
          catch (IOException localIOException1)
          {
            try
            {
              while (true)
              {
                while (true)
                {
                  localBufferedInputStream.close();
                  throw localObject;
                  long l2 = System.currentTimeMillis();
                  l1 = l2;
                }
                l1 = -1L;
              }
              localIOException1 = localIOException1;
              Log.e(null, localIOException1.getMessage(), localIOException1);
            }
            catch (IOException localIOException2)
            {
              while (true)
              {
                while (true)
                {
                  while (true)
                    Log.e(null, localIOException2.getMessage(), localIOException2);
                  localIOException3 = localIOException3;
                  Log.e(null, localIOException3.getMessage(), localIOException3);
                }
                localIOException4 = localIOException4;
                Log.e(null, localIOException4.getMessage(), localIOException4);
              }
            }
          }
        }
      }
    }
  }

  public void a(int paramInt)
  {
    this.j = paramInt;
    h.icon = paramInt;
    h.contentView.setImageViewResource(a("id", "umeng_analyse_appIcon"), paramInt);
  }

  public void a(String paramString)
  {
    h.contentView.setTextViewText(a("id", "umeng_analyse_title"), paramString);
  }

  public void a(boolean paramBoolean)
  {
    this.l = paramBoolean;
  }

  public boolean a()
  {
    return this.k;
  }

  public long b(boolean paramBoolean)
  {
    String str;
    long l1;
    if (paramBoolean)
    {
      str = Environment.getExternalStorageDirectory().toString();
      Log.v(null, "getAvailableStorage. storageDirectory : " + str);
    }
    try
    {
      StatFs localStatFs = new StatFs(str);
      l1 = localStatFs.getAvailableBlocks() * localStatFs.getBlockSize();
      Log.v(null, "getAvailableStorage. avaliableSize : " + l1);
      return l1;
      str = this.b.getFilesDir().getAbsolutePath();
    }
    catch (RuntimeException localRuntimeException)
    {
      while (true)
      {
        Log.e(null, "getAvailableStorage - exception. return 0");
        l1 = 0L;
      }
    }
  }

  public void b(String paramString)
  {
    this.d = paramString;
  }

  public void c()
  {
    if (!(this.k))
      return;
    try
    {
      File localFile = new File(this.d);
      if (!(localFile.exists()))
        localFile.mkdirs();
      new h(this).start();
    }
    catch (Exception localException)
    {
      if (!(UmengConstants.testMode));
      Log.e("MobclickAgent", "Downloading error\n" + localException.getMessage());
    }
  }
}